﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using prohomework.Models;
using System.IO;
using Webdiyer.WebControls.Mvc;

namespace prohomework.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            //if (Session["username"] == null || Session["username"].ToString() == "") return RedirectToAction("Login", "Account");
            //if (Session["usertype"].ToString() == "student")
            //    return RedirectToAction("Index", "Student");
            //else
            //    return RedirectToAction("TeacherIndex", "Teacher");
            string LearnResourcesql = "select top 5 * from Video order by id desc";
            LearnResourseBL LR = new LearnResourseBL();
             ViewData["LearnResource"]=  LR.GetAll(LearnResourcesql);

            string Noticesql = "Select top 5 * from article Where ClassID=1 order by updatetime desc";
            ArticleBL notices = new ArticleBL();
            ViewData["Notice"] = notices.GetAll(Noticesql);

            string TeachingDynamicsql = "Select top 5 * from article Where ClassID=4 order by updatetime desc";
            ArticleBL TeachingDynamics = new ArticleBL();
            ViewData["TeachingDynamic"] = TeachingDynamics.GetAll(TeachingDynamicsql);

            string Referencedocsql = "Select top 5 * from article Where ClassID=4 order by updatetime desc";
            ArticleBL references = new ArticleBL();
            ViewData["referencesdoc"] = TeachingDynamics.GetAll(Referencedocsql);

            string DownLoadfilesql = "select top 6 * from downfile where TypeID<>'1' order by filedate desc";
            DownLoadFileBL downloadfiles = new DownLoadFileBL();
            ViewData["downloadfiles"] = downloadfiles.GetAll(DownLoadfilesql);
            return View();
        }
        #region learnresource

        public ActionResult Video_XZ(string  ID)
        {
            LearnResourseBL GetLR = new LearnResourseBL();
            LearnResource LR = new LearnResource();
            LR.LR_ID = ID;

            GetLR.Update(LR);
           LR= GetLR.GetVideoModel(Convert.ToInt32( ID));

            return View(LR);
        }


        public ActionResult HZ()
        {
            LearnResourseBL LRBL = new LearnResourseBL();
            List<VideoType> videotype = new List<VideoType>();
            videotype = LRBL.GetAllVideoType();
          List<  AllVideoList> allvideolist = new List<AllVideoList>();
            string LearnResourcesql = "select top 5 * from Video order by id desc";
            LearnResourseBL LR = new LearnResourseBL();
            ViewData["LearnResource"] = LR.GetAll(LearnResourcesql);

            foreach (var item in videotype)
            {
                AllVideoList s = new AllVideoList();       
                s.VideoType = item.VT_VideoType_Name;
                string videotypesql = "select top 5 * from Video where Video_Type =" + item.VT_VideoType_Name + " order by ID desc";
                s.LearnResourcelist = LR.GetAll(videotypesql);
                allvideolist.Add(s);
            }
            //tis
            return View(allvideolist);



        }


        #endregion







        #region  downloadfile

        public ActionResult GetFile( int ID)
        {

       //跟新点击数
          //  string updatedownloadnum = "Update downfile Set downNum = isnull(downNum,0) + 1 where id=" + ID;
            DownLoadFile updateobjcet = new DownLoadFile();
            updateobjcet.DLF_ID = ID.ToString();
            DownLoadFileBL toupdatedownloadnum = new DownLoadFileBL();
                toupdatedownloadnum.Update(updateobjcet);

            //获取文件对象
            DownLoadFile s = new DownLoadFile();
            s = toupdatedownloadnum.GetFileModel(ID);
            //File()
            string downpath = "download/" + s.DLF_FileName;
            return Redirect(downpath);

        }





        #endregion




        #region Article 

        public ActionResult ArticleShow(int ID)
        {
            Article updateobject = new Article();
            updateobject.AT_ID = ID.ToString();
            ArticleBL excute = new ArticleBL();
            excute.Update(updateobject);
            Article viewobject = new Article();
            viewobject = excute.GetFileModel(ID);

            return View(viewobject);
        }

        public ActionResult ArticleList(int ClassID, int id = 1, int pagesize = 20)
        {
            string listsql = "Select * from articleView Where ClassID=" + ClassID+" order by updatetime desc";
            ArticleBL DynamicsList = new ArticleBL();
        //   var  ss = new IQueryable<Article>();
           PagedList < Article >ss = (DynamicsList.GetAllList(listsql)).AsQueryable().ToPagedList(id, pagesize); 
            //获取文章类别名称
            ArticleClass dd = new ArticleClass();
            dd= DynamicsList.GetArticleClassModel(ClassID);
            if (dd == null) ViewData["classname"] = "所有文章列表";
            else ViewData["classname"] = dd.AC_Name;

            return View(ss);
        }

        //文章内部嵌入的文章下载
        public ActionResult ArticleInsideDownloadFile(int ID)
        {



            Article s = new Article();
            ArticleBL readdata = new ArticleBL();
              s=   readdata.GetFileModel(ID);
            if (s.AT_fileContentType == "")
            {
                string fileExtension = Path.GetExtension(s.AT_fileName);
                if (fileExtension == ".doc" || fileExtension == ".docx") s.AT_fileContentType = "application/msword";
                else
                {
                    s.AT_fileContentType = "application/octet-stream";

                }
            }
            //   var fattach = new FileContentResult(filedata, "application/octet-stream");  
            //  var ss=new file
            return File(s.AT_pic, s.AT_fileContentType, s.AT_fileName);
      //      return View();
        }

        //文章内部嵌入的图片或者动画显示
        public ActionResult showImageorSwf(int ID)
        {
             Article s = new Article();
            ArticleBL readdata = new ArticleBL();
            s = readdata.GetFileModel(ID);


            return File(s.AT_pic, s.AT_fileContentType);
        }

        #endregion




        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
        public ActionResult LearnResource()
        {


            return View();
        }




    }
}
